package com.isunland.managesystem.ui;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.isunland.managesystem.R;
import com.isunland.managesystem.utils.ToastUtil;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/7/13 13:45
 */
public class BaseEditDialogFragment extends DialogFragment {

  private static final String EXTRA_HINT_ID = "com.isunland.managesystem.ui.EXTRA_HINT_ID";
  private static final String EXTRA_HINT_STRING = "com.isunland.managesystem.ui.EXTRA_STRING";
  private static final String EXTRA_TITLE_ID = "com.isunland.managesystem.ui.EXTRA_TITLE_ID";
  private int mHintId;
  private int mTitleId;
  private String mHintString;
  private CallBack mCallBack;

  @Override public void onCreate(Bundle savedInstanceState) {
    mHintId = getArguments().getInt(EXTRA_HINT_ID);
    mTitleId = getArguments().getInt(EXTRA_TITLE_ID);
    mHintString = getArguments().getString(EXTRA_HINT_STRING);
    super.onCreate(savedInstanceState);
  }

  /**
   * @param hintStringId 提示信息的id
   */
  public static BaseEditDialogFragment newInstance(int hintStringId) {
    Bundle argsBundle = new Bundle();
    argsBundle.putInt(EXTRA_HINT_ID, hintStringId);
    BaseEditDialogFragment fragment = new BaseEditDialogFragment();
    fragment.setArguments(argsBundle);
    return fragment;
  }

  /**
   * @param hintStringId 提示信息的id
   * @param titleId 标题id
   */
  public static BaseEditDialogFragment newInstance(int hintStringId, int titleId) {
    Bundle argsBundle = new Bundle();
    argsBundle.putInt(EXTRA_HINT_ID, hintStringId);
    argsBundle.putInt(EXTRA_TITLE_ID, titleId);
    BaseEditDialogFragment fragment = new BaseEditDialogFragment();
    fragment.setArguments(argsBundle);
    return fragment;
  }

  /**
   * @param hintString 提示信息
   * @param titleId 标题id
   */
  public static BaseEditDialogFragment newInstance(String hintString, int titleId) {
    Bundle argsBundle = new Bundle();
    argsBundle.putString(EXTRA_HINT_STRING, hintString);
    argsBundle.putInt(EXTRA_TITLE_ID, titleId);
    BaseEditDialogFragment fragment = new BaseEditDialogFragment();
    fragment.setArguments(argsBundle);
    return fragment;
  }

  public static BaseEditDialogFragment newInstance(String hintString) {
    Bundle argsBundle = new Bundle();
    argsBundle.putString(EXTRA_HINT_STRING, hintString);
    BaseEditDialogFragment fragment = new BaseEditDialogFragment();
    fragment.setArguments(argsBundle);
    return fragment;
  }

  @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) {
    final View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_edit_text, null);
    final EditText etname = (EditText) view.findViewById(R.id.et_name);
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    if (mTitleId != 0) {
      builder.setTitle(getContext().getResources().getString(mTitleId));
    }
    //if (mHintId != 0) {
    //  builder.setMessage(mHintId);
    //}
    //if (mHintString != null) {
    //  builder.setMessage(mHintString);
    //}

    builder.setView(view)
        .setPositiveButton(R.string.confirm, null)
        .setNegativeButton(R.string.cancel, null);
    final AlertDialog dialog = builder.create();
    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
      @Override public void onShow(DialogInterface dialogInterface) {
        Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
        Button neutralButton = dialog.getButton(AlertDialog.BUTTON_NEUTRAL);

        positiveButton.setOnClickListener(new View.OnClickListener() {

          @Override public void onClick(View v) {
            if (!TextUtils.isEmpty(etname.getText().toString())) {
              mCallBack.select(etname.getText().toString());
              dialog.dismiss();
            } else {
              ToastUtil.show(R.string.notComplete);
            }
          }
        });
        neutralButton.setOnClickListener(new View.OnClickListener() {

          @Override public void onClick(View v) {
            dialog.dismiss();
          }
        });
      }
    });
    //return builder.create();
    return dialog;
  }

  public BaseEditDialogFragment setCallBack(CallBack callBack) {
    mCallBack = callBack;
    return this;
  }

  public interface CallBack {
    void select(String message);
  }
}
